package com.reabam.tryshopping.h5.reabampos;

import androidx.annotation.NonNull;
import android.app.Activity;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import android.util.Log;
//import hyl.xsdk.sdk.api.android.utils.XNumberUtils;

/** 自定义临时android插件 */
public class AndroidPlugin implements MethodCallHandler {

  public static final String CHANNEL = "xsdk_channel";
  static MethodChannel channel;

  private Activity activity;
  private AndroidPlugin(Activity activity) {
    this.activity = activity;
  }

  public static void registerWith(FlutterEngine flutterEngine, FlutterActivity activity) {
    channel = new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL);
    AndroidPlugin instance = new AndroidPlugin(activity);
    //setMethodCallHandler在此通道上接收方法调用的回调
    channel.setMethodCallHandler(instance);
  }

  @Override
  public void onMethodCall(MethodCall call, MethodChannel.Result result) {
    //通过MethodCall可以获取参数和方法名，然后再寻找对应的平台业务，本案例做了2个跳转的业务
    //接收来自flutter的指令oneAct
    if (call.method.equals("trun2Second")) {
      //返回给flutter的参数
      result.success("success");
    }else if (call.method.equals("flutter_test_1")) {
      if (call.arguments != null) {
        Log.e("XSDK", "Flutter -> Android 回调内容：" + call.arguments.toString());

//        double a=3.141592654;
//        double b=XNumberUtils.d2d(2,a);
//        Log.e("XSDK","b="+b);

      } else {
        Log.e("XSDK", "Flutter -> Android 回调参数为空！");
      }
    }
    else if (call.method.equals("getNativeResult")) {
      //解析参数

//      //android调用flutter方法 并获取结果
//      channel.invokeMethod("flutterMedia", "from android", new MethodChannel.Result() {
//        @Override
//        public void success(Object o) {
//          // 这里就会输出 "Hello from Flutter"
//          Log.i(">>>>>>>>>>debug", o.toString());
//        }
//        @Override
//        public void error(String s, String s1, Object o) {
//        }
//
//        @Override
//        public void notImplemented() {
//
//        }
//      });
    }else if(call.method.equals("goBack")){
      activity.finish();
    }
    else {
      result.notImplemented();
    }
  }
}
